-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[mlir][Transforms] More detailed error message when new IR cannot be legalized #152297
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[mlir][Transforms] More detailed error message when new IR cannot be legalized #152297
Conversation
@llvm/pr-subscribers-mlir Author: Matthias Springer (matthias-springer) ChangesPrint a more detailed error message when new/modified IR could not be legalized with Full diff: https://github.com/llvm/llvm-project/pull/152297.diff 1 Files Affected:
diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp b/mlir/lib/Transforms/Utils/DialectConversion.cpp
index f23c6197accd5..463394c19b51c 100644
--- a/mlir/lib/Transforms/Utils/DialectConversion.cpp
+++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp
@@ -2302,6 +2302,37 @@ OperationLegalizer::legalizeWithFold(Operation *op,
return success();
}
+/// Report a fatal error indicating that newly produced or modified IR could
+/// not be legalized.
+static void
+reportNewIrLegalizationFatalError(const Pattern &pattern,
+ const SetVector<Operation *> &newOps,
+ const SetVector<Operation *> &modifiedOps,
+ const SetVector<Block *> &insertedBlocks) {
+ StringRef detachedBlockStr = "(detached block)";
+ std::string newOpNames = llvm::join(
+ llvm::map_range(
+ newOps, [](Operation *op) { return op->getName().getStringRef(); }),
+ ", ");
+ std::string modifiedOpNames = llvm::join(
+ llvm::map_range(
+ newOps, [](Operation *op) { return op->getName().getStringRef(); }),
+ ", ");
+ std::string insertedBlockNames = llvm::join(
+ llvm::map_range(insertedBlocks,
+ [&](Block *block) {
+ if (block->getParentOp())
+ return block->getParentOp()->getName().getStringRef();
+ return detachedBlockStr;
+ }),
+ ", ");
+ llvm::report_fatal_error(
+ "pattern '" + pattern.getDebugName() +
+ "' produced IR that could not be legalized. " + "new ops: {" +
+ newOpNames + "}, " + "modified ops: {" + modifiedOpNames + "}, " +
+ "inserted block into ops: {" + insertedBlockNames + "}");
+}
+
LogicalResult
OperationLegalizer::legalizeWithPattern(Operation *op,
ConversionPatternRewriter &rewriter) {
@@ -2389,8 +2420,8 @@ OperationLegalizer::legalizeWithPattern(Operation *op,
appliedPatterns.erase(&pattern);
if (failed(result)) {
if (!rewriterImpl.config.allowPatternRollback)
- llvm::report_fatal_error("pattern '" + pattern.getDebugName() +
- "' produced IR that could not be legalized");
+ reportNewIrLegalizationFatalError(pattern, newOps, modifiedOps,
+ insertedBlocks);
rewriterImpl.resetState(curState, pattern.getDebugName());
}
if (config.listener)
|
@llvm/pr-subscribers-mlir-core Author: Matthias Springer (matthias-springer) ChangesPrint a more detailed error message when new/modified IR could not be legalized with Full diff: https://github.com/llvm/llvm-project/pull/152297.diff 1 Files Affected:
diff --git a/mlir/lib/Transforms/Utils/DialectConversion.cpp b/mlir/lib/Transforms/Utils/DialectConversion.cpp
index f23c6197accd5..463394c19b51c 100644
--- a/mlir/lib/Transforms/Utils/DialectConversion.cpp
+++ b/mlir/lib/Transforms/Utils/DialectConversion.cpp
@@ -2302,6 +2302,37 @@ OperationLegalizer::legalizeWithFold(Operation *op,
return success();
}
+/// Report a fatal error indicating that newly produced or modified IR could
+/// not be legalized.
+static void
+reportNewIrLegalizationFatalError(const Pattern &pattern,
+ const SetVector<Operation *> &newOps,
+ const SetVector<Operation *> &modifiedOps,
+ const SetVector<Block *> &insertedBlocks) {
+ StringRef detachedBlockStr = "(detached block)";
+ std::string newOpNames = llvm::join(
+ llvm::map_range(
+ newOps, [](Operation *op) { return op->getName().getStringRef(); }),
+ ", ");
+ std::string modifiedOpNames = llvm::join(
+ llvm::map_range(
+ newOps, [](Operation *op) { return op->getName().getStringRef(); }),
+ ", ");
+ std::string insertedBlockNames = llvm::join(
+ llvm::map_range(insertedBlocks,
+ [&](Block *block) {
+ if (block->getParentOp())
+ return block->getParentOp()->getName().getStringRef();
+ return detachedBlockStr;
+ }),
+ ", ");
+ llvm::report_fatal_error(
+ "pattern '" + pattern.getDebugName() +
+ "' produced IR that could not be legalized. " + "new ops: {" +
+ newOpNames + "}, " + "modified ops: {" + modifiedOpNames + "}, " +
+ "inserted block into ops: {" + insertedBlockNames + "}");
+}
+
LogicalResult
OperationLegalizer::legalizeWithPattern(Operation *op,
ConversionPatternRewriter &rewriter) {
@@ -2389,8 +2420,8 @@ OperationLegalizer::legalizeWithPattern(Operation *op,
appliedPatterns.erase(&pattern);
if (failed(result)) {
if (!rewriterImpl.config.allowPatternRollback)
- llvm::report_fatal_error("pattern '" + pattern.getDebugName() +
- "' produced IR that could not be legalized");
+ reportNewIrLegalizationFatalError(pattern, newOps, modifiedOps,
+ insertedBlocks);
rewriterImpl.resetState(curState, pattern.getDebugName());
}
if (config.listener)
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM with one change
Co-authored-by: Jeremy Kun <[email protected]>
✅ With the latest revision this PR passed the C/C++ code formatter. |
8f7faa2
to
73dfec2
Compare
Print a more detailed error message when new/modified IR could not be legalized with
allowPatternRollback = false
. This is useful to understand why a pattern is incompatible with the new One-Shot Dialect Conversion driver.